package com.dididoctor.doctor.Http;

import android.content.Context;
import android.net.Uri;
import android.util.Log;

import com.dididoctor.doctor.Activity.ConstantValue;
import com.dididoctor.doctor.Bean.Token;
import com.dididoctor.doctor.Utils.ABFileUtil;
import com.dididoctor.doctor.Utils.imageprocess.ABImageProcess;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import java.io.File;

/**
 * Created by mahuihuang on 15/4/11.
 */
public class FileResourceClient {

    private static String TAG = "FileResourceClient";

    private static AsyncHttpClient client = new AsyncHttpClient();

    public static void uploadToFileService(Context context, AsyncHttpResponseHandler responseHandler, String... path) {
        if (path == null || path.length == 0) {
            Log.w(TAG, "没有要上传的文件");
            return;
        }
        RequestParams params = new RequestParams();
        for (int i = 0; i < path.length; i++) {
            try {
                File file = new File(path[i]);
                //对文件进行压缩处理
                ABImageProcess.compressImage2SD(file, path[i], 712, 960, 85);
                params.put("profile_picture" + i, new File(path[i]));
            } catch (Exception e) {
                Log.e(TAG, "没有找到文件", e);
            }
        }
        client.post(ConstantValue.ServerURL + "certPicUpload.json", params, responseHandler);
    }

    public static void uploadToFileService(Context context, AsyncHttpResponseHandler responseHandler, Uri... uris) {
        if (uris == null || uris.length == 0) {
            Log.w(TAG, "没有要上传的文件");
            return;
        }
        RequestParams params = new RequestParams();
        for (int i = 0; i < uris.length; i++) {
            try {
                params.put("profile_picture" + i, ABFileUtil.uri2File(context, uris[i]));
            } catch (Exception e) {
                Log.e(TAG, "没有找到文件", e);
            }
        }

        client.post(ConstantValue.ServerURL + "certPicUpload.json", params, responseHandler);
    }

    public static void uploadToAppService(Context context, AsyncHttpResponseHandler responseHandler, String... path) {
        if (path == null || path.length == 0) {
            Log.w(TAG, "没有要上传的文件");
            return;
        }
        RequestParams params = new RequestParams();
        for (int i = 0; i < path.length; i++) {
            try {
                File file = new File(path[i]);
                //对文件进行压缩处理
                ABImageProcess.compressImage2SD(file, path[i], 712, 960, 85);
//                params.put("profile_picture" + i, new File(path[i]));
                params.put("file", file);
                params.put("userId", Token.getUserId());
                params.put("token", Token.getToken());
            } catch (Exception e) {
                Log.e(TAG, "没有找到文件:" + path[i], e);
                if (responseHandler != null) {
                    responseHandler.onFailure(-1, null, null, e);
                }
                return;
            }
        }
//        userId	String 	是	用户id
//        token	string	是
//        file	MultipartFile	是	资格证图片
        client.post(ConstantValue.ServerURL + "certPicUpload.json", params, responseHandler);

    }

}
